<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="80px">
      <el-form-item label="影院名称" prop="cinemaName">
        <el-input
          v-model="queryParams.cinemaName"
          placeholder="请输入电影院名称"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话" prop="cinemaPhone">
        <el-input
          v-model="queryParams.cinemaPhone"
          placeholder="请输入电话"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="省" prop="cinemaProvince">
        <el-input
          v-model="queryParams.cinemaProvince"
          placeholder="请输入影院所在的省"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="城市" prop="cinemaCity">
        <el-input
          v-model="queryParams.cinemaCity"
          placeholder="请输入影院所在城市"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区县" prop="cinemaArea">
        <el-input
          v-model="queryParams.cinemaArea"
          placeholder="请输入区县"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="详细地址" prop="cinemaLocation">
        <el-input
          v-model="queryParams.cinemaLocation"
          placeholder="请输入详细地址"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="品牌" prop="cinemaBrand">
        <el-input
          v-model="queryParams.cinemaBrand"
          placeholder="请输入品牌"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="影院照片" prop="cinemaImg">-->
<!--        <el-input-->
<!--          v-model="queryParams.cinemaImg"-->
<!--          placeholder="请输入影院照片"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

<!--      <el-form-item label="经度" prop="cinemaLng">-->
<!--        <el-input-->
<!--          v-model="queryParams.cinemaLng"-->
<!--          placeholder="请输入经度"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="纬度" prop="cinemaLat">-->
<!--        <el-input-->
<!--          v-model="queryParams.cinemaLat"-->
<!--          placeholder="请输入纬度"-->
<!--          clearable-->
<!--          size="small"-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['cinema:cinema:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['cinema:cinema:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['cinema:cinema:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['cinema:cinema:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="cinemaList" @selection-change="handleSelectionChange">
<!--      <el-table-column type="selection" width="55" align="center" />-->
<!--      <el-table-column label="电影院id" align="center" prop="cinemaId" />-->
      <el-table-column label="影院名称" align="center" prop="cinemaName" />
      <el-table-column label="电话" align="center" prop="cinemaPhone" />
      <el-table-column label="省" align="center" prop="cinemaProvince" :formatter="convertProvince" />
      <el-table-column label="城市" align="center" prop="cinemaCity"  :formatter="convertCity"/>
      <el-table-column label="区县" align="center" prop="cinemaArea"  :formatter="convertCounty"/>
      <el-table-column label="详细地址" align="center" prop="cinemaLocation" />
      <el-table-column label="品牌" align="center" prop="cinemaBrand" :formatter="convertBrand"/>
      <el-table-column label="照片" align="center" prop="cinemaImg" >
        <template slot-scope="scope">
          <el-image
            style="width: 100px; height: 100px"
            :src="scope.row.cinemaImg"></el-image>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['cinema:cinema:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['cinema:cinema:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改电影院信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="品牌" prop="cinemaBrand">
          <el-select v-model="form.cinemaBrand" placeholder="请选择">
            <el-option
              v-for="item in brandList"
              :key="item.brandId"
              :label="item.brandName"
              :value="item.brandId">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.cinemaBrand" placeholder="请输入品牌" />-->
        </el-form-item>
        <el-form-item label="影院名称" prop="cinemaName">
          <el-input v-model="form.cinemaName" placeholder="请输入电影院名称" />
        </el-form-item>
        <el-form-item label="所在的省" prop="cinemaProvince">
          <el-select v-model="form.cinemaProvince" placeholder="请选择" @change="getCityListByProvince">
            <el-option
              v-for="item in provinceList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
            </el-select>
        </el-form-item>
        <el-form-item label="所在城市" prop="cinemaCity">
          <el-select v-model="form.cinemaCity" placeholder="请选择" @change="getCountyListByCity">
            <el-option
              v-for="item in cityList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.cinemaCity" placeholder="请输入影院所在城市" />-->
        </el-form-item>
        <el-form-item label="区县" prop="cinemaArea">
          <el-select v-model="form.cinemaArea" placeholder="请选择">
            <el-option
              v-for="item in countyList"
              :key="item.id"
              :label="item.name"
              :value="item.id">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.cinemaArea" placeholder="请输入区县" />-->
        </el-form-item>

        <el-form-item label="详细地址" prop="cinemaLocation">
          <el-input v-model="form.cinemaLocation" placeholder="请输入详细地址" />
        </el-form-item>
        <el-form-item label="电话" prop="cinemaPhone">
          <el-input v-model="form.cinemaPhone" placeholder="请输入电话" />
        </el-form-item>

        <el-form-item label="影院照片" prop="cinemaImg">
<!--          <el-input v-model="form.cinemaImg" placeholder="请输入影院照片" />-->
          <el-upload
            action="http://localhost:8888/upload"
            :headers="myHeaders"
            list-type="picture-card"
            :file-list="uploadImageFileList"
            :on-success="uploadSuccess"
            :on-preview="handlePictureCardPreview"
            :on-remove="handleRemove">
            <i class="el-icon-plus"></i>
          </el-upload>
          <el-dialog :visible.sync="dialogVisible">
            <img width="100%" :src="dialogImageUrl" alt="">
          </el-dialog>
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listCinema, getCinema, delCinema, addCinema, updateCinema, exportCinema } from "@/api/cinema/cinema";
import { listBrand } from "@/api/cinema/brand";
import { getToken } from '@/utils/auth'
import areas from "@/utils/areas.js"
export default {
  name: "Cinema",
  components: {
  },
  data() {
    return {
      uploadImageFileList:[],//回显上传过的图片
      myHeaders:{},
      dialogImageUrl: '',
      dialogVisible: false,
      provinceList:[],//省列表
      cityList:[],//城市列表
      countyList:[],//区县列表
      brandList:[],//品牌列表
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      fits: ['fill', 'contain', 'cover', 'none', 'scale-down'],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 电影院信息表格数据
      cinemaList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        cinemaName: null,
        cinemaPhone: null,
        cinemaArea: null,
        cinemaLocation: null,
        cinemaBrand: null,
        cinemaImg: null,
        cinemaCity: null,
        cinemaLng: null,
        cinemaLat: null,
        cinemaProvince: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {

    this.getList();
    this.getAllBrandList();
    this.getAllProvinceList();
    this.getJwtToken();

  },
  methods: {
    getJwtToken(){
      let token = 'Bearer ' + getToken();
      this.myHeaders={'Authorization':token}
    },
    uploadSuccess(response, file, fileList){
      console.log('uploadSuccess',response);
      this.form.cinemaImg=response;
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    //根据省查询城市
    getCityListByProvince(){
      // form.cinemaProvince
      this.cityList=[];
      this.countyList=[];
      this.form.cinemaCity=undefined;
      this.form.cinemaArea=undefined;
      areas.forEach(c=>{
        if(c.parentId==this.form.cinemaProvince){
          this.cityList.push(c);
        }
      })
    },
    //根据城市查询区县
    getCountyListByCity(){
      this.countyList=[];
      this.form.cinemaArea=undefined;
      areas.forEach(c=>{
        if(c.parentId==this.form.cinemaCity){
          this.countyList.push(c);
        }
      })
    },
    //获取省列表
    getAllProvinceList(){
      areas.forEach(p=>{
        if(p.type==1){
          this.provinceList.push(p);
        }
      })
    },
    //查询所有品牌列表
    getAllBrandList(){
      listBrand({pageNum:1,pageSize:1000}).then(res=>{
        console.log('getAllBrandList',res);
        this.brandList=res.rows;
      })
    },
    convertBrand(row, column, cellValue, index){
      for(let brand of this.brandList){
        if(cellValue==brand.brandId){
          return brand.brandName
        }
      }
    },
    //把表格中省的id转成名称
    convertProvince(row, column, cellValue, index){
      console.log('province id',cellValue)
      for(let area of areas){
          if(cellValue==area.id){
            return area.name
          }
      }

    },
    //把表格中城市的id转成名称
    convertCity(row, column, cellValue, index){
      for(let area of areas){
        if(cellValue==area.id){
          return area.name
        }
      }
    },
    //把表格中区县的id转成名称
    convertCounty(row, column, cellValue, index){
      for(let area of areas){
        if(cellValue==area.id){
          return area.name
        }
      }
    },
    /** 查询电影院信息列表 */
    getList() {
      this.loading = true;
      listCinema(this.queryParams).then(response => {
        this.cinemaList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.uploadImageFileList=[];
      this.form = {
        cinemaId: null,
        cinemaName: null,
        cinemaPhone: null,
        cinemaArea: null,
        cinemaLocation: null,
        cinemaBrand: null,
        cinemaImg: null,
        cinemaCity: null,
        cinemaLng: null,
        cinemaLat: null,
        cinemaProvince: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.cinemaId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加电影院信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const cinemaId = row.cinemaId || this.ids
      getCinema(cinemaId).then(response => {
        let city=response.data.cinemaCity;
        let county=response.data.cinemaArea;

        this.form = response.data;
        this.open = true;
        this.title = "修改电影院信息";

        //把图片的地址放入到图片空间的fileList中
        this.uploadImageFileList=[{name: this.form.cinemaImg, url: this.form.cinemaImg}]

        this.getCityListByProvince();
        console.log(this.form.cinemaProvince)
        this.form.cinemaCity=city
        console.log(city)
        this.getCountyListByCity();
        this.form.cinemaArea=county;
        console.log(county)
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.cinemaId != null) {
            updateCinema(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addCinema(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const cinemaIds = row.cinemaId || this.ids;
      this.$confirm('是否确认删除电影院信息编号为"' + cinemaIds + '"的数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delCinema(cinemaIds);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有电影院信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportCinema(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
